package com.sufun.adapters;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.ab.view.pullview.AbPullToRefreshView;
import com.ab.view.pullview.AbPullToRefreshView.OnFooterLoadListener;
import com.ab.view.pullview.AbPullToRefreshView.OnHeaderRefreshListener;
import com.sufun.base.SFConfig;
import com.sufun.fetches.IFetch;
import com.sufun.fetches.SFBaseFetch;
import com.sufun.models.DataResultModel;
/**
 * 基础的适配器
 * @author Wu
 *
 * @param <T>
 */
public abstract class SFBaseAdapter<T> extends BaseAdapter implements OnHeaderRefreshListener,OnFooterLoadListener{

	public List<T> mDatas;
	/**
	 * 下拉刷新控件
	 */
	private AbPullToRefreshView mRefreshView;
	/**
	 * 列表控件
	 */
	private ListView mListView;
	/**
	 * 上下文
	 */
	public Context mContext;
	/**
	 * 取数据接口
	 */
	private SFBaseFetch<T>  baseFetch;
	/**
	 * 数据回调接口,暴露给外界的
	 */
	public IFetch<T> fetchAnsyCallBackFetch;
	/**
	 * 
	 * @param context
	 * @param list_data
	 *            模型列表
	 */
	public void initAdapterDatas(SFBaseAdapterConfig config) {
		mContext = config.mContext;
		mDatas = config.mDatas;
		mRefreshView=config.mRefreshView;
		mListView=config.mListView;
		
		mRefreshView.setOnHeaderRefreshListener(this);
		mRefreshView.setOnFooterLoadListener(this);
		
		// 设置进度条的样式
//		mRefreshView.getHeaderView().setHeaderProgressBarDrawable(
//				this.getResources().getDrawable(R.drawable.progress_circular));
//		mRefreshView.getFooterView().setFooterProgressBarDrawable(
//				this.getResources().getDrawable(R.drawable.progress_circular));
		
		//绑定适配器
		mListView.setAdapter(this);
		baseFetch=config.baseFetch;
		fetchAnsyCallBackFetch=config.fetchansyCallback;
		initAnsyCallback();
		
	}
    /**
     * 初始化回调接口
     */
	private void initAnsyCallback()
	{
	    baseFetch.mFetchAnsyCallback=new IFetch<T>() {

			@Override
			public void OnLoadMore() {
				// TODO Auto-generated method stub
				if (fetchAnsyCallBackFetch!=null) {
					fetchAnsyCallBackFetch.OnLoadMore();					
				}
			}

			@Override
			public void OnPrepare() {
				// TODO Auto-generated method stub
				if (fetchAnsyCallBackFetch!=null) {
					fetchAnsyCallBackFetch.OnPrepare();
				}
			}

			@Override
			public void OnLoading(int data) {
				// TODO Auto-generated method stub
				if (fetchAnsyCallBackFetch!=null) {
					fetchAnsyCallBackFetch.OnLoading(data);
				}
			}

			@Override
			public void OnSuccess(List<T> datas) {
				// TODO Auto-generated method stub
				if (fetchAnsyCallBackFetch!=null) {
					fetchAnsyCallBackFetch.OnSuccess(datas);
				}
				mDatas.clear();
				mDatas.addAll(datas);
			    notifyDataSetChanged();
			    mRefreshView.onHeaderRefreshFinish();
			    mRefreshView.onFooterLoadFinish();
			}

			@Override
			public void OnFail(DataResultModel result) {
				// TODO Auto-generated method stub
				if (fetchAnsyCallBackFetch!=null) {
					fetchAnsyCallBackFetch.OnFail(result);
				}
			    mRefreshView.onHeaderRefreshFinish();
			    mRefreshView.onFooterLoadFinish();
			}
			
			@Override
			public void OnFinish() {				
				if (fetchAnsyCallBackFetch!=null) {
					fetchAnsyCallBackFetch.OnFinish();
				}
			    mRefreshView.onHeaderRefreshFinish();
			    mRefreshView.onFooterLoadFinish();
			};

			@Override
			public void OnReFresh() {
				// TODO Auto-generated method stub
				if (fetchAnsyCallBackFetch!=null) {
					fetchAnsyCallBackFetch.OnReFresh();
				}
			}
		};
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mDatas.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public abstract View getView(int position, View convertView,
			ViewGroup parent);

	@Override
	public void notifyDataSetChanged() {
		// TODO Auto-generated method stub
		super.notifyDataSetChanged();
	}
	/**
	 * 底部数据加载
	 */
	@Override
	public void onFooterLoad(AbPullToRefreshView arg0) {
		// TODO Auto-generated method stub
		baseFetch.OnLoadMore();
	}
	/**
	 * 头部数据加载
	 */
	@Override
	public void onHeaderRefresh(AbPullToRefreshView arg0) {
		// TODO Auto-generated method stub
		baseFetch.OnReFresh();
	}
	

}
